package com.igoal.web;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.igoal.dto.WallActivityDTO;
import com.igoal.service.UserManagerExt;
import com.igoal.utils.ConvertUtils;
import com.igoal.web.model.BaseModel;

@Controller
@RequestMapping("/ajax/wallActivities.html")
public class AjaxWallActivitiesController extends BaseAjaxController {
	private final static Log logger = LogFactory.getLog(AjaxActivityViewController.class);
	
	@Autowired
	public void setUserManagerExt(UserManagerExt userManagerExt) {
		this.userManagerExt = userManagerExt;
	}

	@RequestMapping(method = RequestMethod.GET)
	public String show(ModelMap modelMap,
			@RequestParam("lastOrderValue") String lastOrderValue,
			@RequestParam("wallOwner") String wallOwner) {
		BaseModel data = new BaseModel();
		initSessionInformation(data);
		
		List<WallActivityDTO> activities = new ArrayList<WallActivityDTO>();
		
		try {
			long lastValue = ConvertUtils.toLong(lastOrderValue, -1l);

			// get wall activities
			activities = actionLoggerService.getWallActivities(
					getUserInSession(), wallOwner, lastValue);
		} catch (Exception e) {
			logger.error(e);
		}

		modelMap.addAttribute("activities", activities);
		modelMap.addAttribute("wallOwner", wallOwner);
		applyModel(modelMap, data);
		return getViewPrefix() + "ajaxWallActivities";
	}
}
